<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="Model">
		<xsl:copy>
			<xsl:apply-templates select="//Package"/>
			<xsl:apply-templates select="//Class[@inherit!='']" mode="inheritance"/>
			<xsl:apply-templates select="//Class[@implement!='']" mode="implementation"/>
			<xsl:apply-templates select="//Relationship"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="Package"> 
		<xsl:copy>
			<xsl:copy-of select="@name"/>
			<xsl:apply-templates select="//Class"/>
			<xsl:apply-templates select="//Interface"/>
			<xsl:apply-templates select="//Singleton"/>
			<xsl:apply-templates select="//Enumeration"/>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="Class">
		<xsl:element name="Class">

			<xsl:copy-of select="@name"/>

			<xsl:attribute name="abstract">false</xsl:attribute>
			<xsl:attribute name="visibility">v_public</xsl:attribute>
			<xsl:attribute name="final">false</xsl:attribute>
			<xsl:attribute name="stereotype"></xsl:attribute>
			<xsl:attribute name="associationClass"><xsl:apply-templates select="." mode="IsAssociationClass"/></xsl:attribute>
			<xsl:attribute name="documentation"></xsl:attribute>

			<xsl:apply-templates/>
			
		</xsl:element>
	</xsl:template>

	<xsl:template match="Property">
		<xsl:element name="Property">

			<xsl:copy-of select="@name"/>
			<xsl:copy-of select="@type"/>

			<xsl:attribute name="visibility">v_public</xsl:attribute>
			<xsl:attribute name="documentation"></xsl:attribute>
			<xsl:attribute name="static"></xsl:attribute>
			<xsl:attribute name="defaultValue"></xsl:attribute>

			<xsl:apply-templates/>
			
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="Interface">
		<xsl:element name="Interface">
		
			<xsl:copy-of select="@name"/>
			
			<xsl:attribute name="documentation"></xsl:attribute>
			<xsl:attribute name="visibility">v_public</xsl:attribute>
			<xsl:attribute name="final">false</xsl:attribute>
		
			<xsl:apply-templates/>
			
		</xsl:element>
	</xsl:template>


	<xsl:template match="Class" mode="inheritance">
		<xsl:element name="Generalization">
			<xsl:attribute name="name"></xsl:attribute>
			<xsl:attribute name="documentation"></xsl:attribute>
			<xsl:attribute name="clientClassName"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="supplierClassName"><xsl:value-of select="@inherit"/></xsl:attribute>
		</xsl:element>
	</xsl:template>
	
	
	<xsl:template match="Class" mode="implementation">
		
		<xsl:variable name="Tmp_ImplementedInterfaces">
			<xsl:call-template name="TokenizeInterfaceList">
				<xsl:with-param name="interfacesList" select="@implement"/>
			</xsl:call-template>
		</xsl:variable>
	
		<xsl:variable name="ImplementedInterfaces" select="$Tmp_ImplementedInterfaces"/>
		<xsl:variable name="className" select="@name"/>
		
		<xsl:for-each select="msxsl:node-set($ImplementedInterfaces)//interface">
			<xsl:element name="Implementation">
				<xsl:attribute name="name"></xsl:attribute>
				<xsl:attribute name="documentation"></xsl:attribute>
				<xsl:attribute name="clientClassName"><xsl:value-of select="$className"/></xsl:attribute>
				<xsl:attribute name="supplierClassName"><xsl:value-of select="@name"/></xsl:attribute>
			</xsl:element>
		</xsl:for-each>
		
	</xsl:template>
	
	
	<xsl:template name="TokenizeInterfaceList">
		<xsl:param name="interfacesList"/>

		<xsl:if test="string-length($interfacesList) > 0">
			<xsl:element name="interface">
				<xsl:variable name="firstInterface" select="substring-before($interfacesList, ',')"/>
				
				<xsl:choose>
					<xsl:when test="$firstInterface = '' ">
						<xsl:attribute name="name"><xsl:value-of select="translate($interfacesList, ' ', '')"/></xsl:attribute>
					</xsl:when>
					
					<xsl:otherwise>
						<xsl:attribute name="name"><xsl:value-of select="translate($firstInterface, ' ', '') "/></xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
				
			</xsl:element>
			
			<xsl:call-template name="TokenizeInterfaceList">
				<xsl:with-param name="interfacesList" select="substring-after($interfacesList, ',')"/>
			</xsl:call-template>
		</xsl:if>
		
	</xsl:template>
	
	
	<xsl:template match="Singleton">
		<xsl:element name="Class">

			<xsl:copy-of select="@name"/>

			<xsl:attribute name="abstract">false</xsl:attribute>
			<xsl:attribute name="visibility">v_public</xsl:attribute>
			<xsl:attribute name="final">false</xsl:attribute>
			<xsl:attribute name="stereotype">Singleton</xsl:attribute>
			<xsl:attribute name="associationClass">false</xsl:attribute>
			<xsl:attribute name="documentation"></xsl:attribute>

			<xsl:apply-templates/>
			
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="Enumeration">
		<xsl:element name="Enumeration">
			<xsl:copy-of select="@name"/>
			
			<xsl:attribute name="documentation"></xsl:attribute>
			<xsl:attribute name="visibility"></xsl:attribute>
			<xsl:attribute name="final"></xsl:attribute>

			<xsl:apply-templates/>

		</xsl:element>
	</xsl:template>

	<xsl:template match="Literal">
		<xsl:element name="Property">
			<xsl:copy-of select="@name"/>
			
			<xsl:attribute name="documentation"></xsl:attribute>
			<xsl:attribute name="type"></xsl:attribute>
			<xsl:attribute name="visibility"></xsl:attribute>
			<xsl:attribute name="static"></xsl:attribute>
			<xsl:attribute name="defaultValue"></xsl:attribute>

		</xsl:element>
	</xsl:template>

	
	<xsl:template match="Relationship">
		<xsl:element name="Association">
			<xsl:copy-of select="@type"/>
			
			<xsl:attribute name="name"><xsl:value-of select="./LinkClass/@name"/></xsl:attribute>
			<xsl:attribute name="documentation"></xsl:attribute>
			<xsl:attribute name="navigable">true</xsl:attribute>
			<xsl:attribute name="associationClassifier"><xsl:value-of select="./LinkClass/@name"/></xsl:attribute>

			<xsl:attribute name="clientClassName"><xsl:value-of select="./Parent/@name"/></xsl:attribute>
			<xsl:attribute name="clientRole"><xsl:value-of select="./Parent/@role"/></xsl:attribute>
			<xsl:attribute name="clientMultiplicity"><xsl:value-of select="./Parent/@multiplicity"/></xsl:attribute>
			<xsl:attribute name="clientQualifierName"></xsl:attribute>
			<xsl:attribute name="clientQualifierType"></xsl:attribute>

			<xsl:attribute name="supplierClassName"><xsl:value-of select="./Child/@name"/></xsl:attribute>
			<xsl:attribute name="supplierRole"><xsl:value-of select="./Child/@role"/></xsl:attribute>
			<xsl:attribute name="supplierMultiplicity"><xsl:value-of select="./Child/@multiplicity"/></xsl:attribute>
			<xsl:attribute name="supplierQualifierName"></xsl:attribute>
			<xsl:attribute name="supplierQualifierType"></xsl:attribute>
			
		</xsl:element>
	</xsl:template>

	<!-- Define wheter or not a class is an "associationClass"-->
	<xsl:template match="Class" mode="IsAssociationClass">
		<xsl:variable name="className" select="@name"/>
		<xsl:choose>
			<xsl:when test="//Relationship/LinkClass[@name=$className]">true</xsl:when>
			<xsl:otherwise>false</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
